home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / test / regex.c < prev    next >
C/C++ Source or Header  |  1990-05-29  |  3KB  |  97 lines

  1. /* Test class Regex
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     K. E. Gorlen
  12.     Bg. 12A, Rm. 2033
  13.     Computer Systems Laboratory
  14.     Division of Computer Research and Technology
  15.     National Institutes of Health
  16.     Bethesda, Maryland 20892
  17.     Phone: (301) 496-1111
  18.     uucp: uunet!nih-csl!kgorlen
  19.     Internet:kgorlen@alw.nih.gov
  20.  
  21. Function:
  22.     
  23. Modification History:
  24.     
  25. $Log:    regex.c,v $
  26.  * Revision 3.0  90/05/29  13:46:19  kgorlen
  27.  * Release for 1st edition.
  28.  * 
  29. */
  30. static char rcsid[] = "$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/test/RCS/regex.c,v 3.0 90/05/29 13:46:19 kgorlen Rel $";
  31.  
  32. #include "Regex.h"
  33. #include "OIOnih.h"
  34. #include <fcntl.h>
  35. #include <osfcn.h>
  36.  
  37. void printGroups(const Regex& r, const String& s, ostream& strm =cout)
  38. {
  39.     for (int i=0; i<r.groups(); i++) {
  40.         strm << '\\' << i << ":    " << ((String&)s)(r[i])
  41.             << "    [" << s << '(' << r[i] << ")]" << endl;
  42.     }
  43. }
  44.  
  45. void testPattern(Regex& r, const String& s, ostream& strm =cout)
  46. {
  47.     strm << r << " match(" << s << ")? "
  48.         << (r.match(s) ? "YES" : "NO") << endl;
  49.     printGroups(r,s);
  50.     strm << r << " search(" << s << ")? "
  51.         << (r.search(s) != -1 ? "YES" : "NO") << endl;
  52.     printGroups(r,s);
  53.     strm << r << " search(" << s << ',' << s.length()-1 << ',' << -(int)s.length()+1 << ")? "
  54.         << (r.search(s,s.length()-1,-s.length()+1) != -1 ? "YES" : "NO") << endl;
  55.     printGroups(r,s);
  56.     cout << "---" << endl;
  57. }
  58.  
  59. const char* fileName = "regex.obj";
  60.  
  61. main()
  62. {
  63.     cout << "\nTest Class Regex" << endl;
  64.     Regex r = "\\([^a-zA-Z]*\\)\\([a-zA-Z]+\\)\\([^a-zA-Z]*\\)";
  65.     testPattern(r,"123First123456789");
  66.     Regex r2 = r;
  67.     testPattern(r2,"123First123456789");
  68.     Regex r3;
  69.     r3 = r;
  70.     testPattern(r3,"123First123456789");
  71.     ostream* out = new ostream(creat(fileName,0664));
  72.     r3.storeOn(OIOnihout(*out));
  73.     delete out;
  74.     istream* in = new istream(open(fileName,O_RDONLY));
  75.     Regex* rp = Regex::readFrom(OIOnihin(*in));
  76.     delete in;
  77.     testPattern(*rp,"123First123456789");
  78.  
  79.     String s;
  80.     while (YES) {
  81.         cout << "Enter PATTERN: "; cin >> r;
  82.         if (cin.eof()) {
  83.             cout << endl;
  84.             exit(0);
  85.         }
  86.         while (YES) {
  87.             cout << "Enter STRING: "; cin >> s;
  88.             if (cin.eof()) {
  89.                 cout << endl;
  90.                 exit(0);
  91.             }
  92.             if (s.length()==0) break;
  93.             testPattern(r,s);
  94.         }
  95.     }
  96. }
  97.